package com.hnkywl.sts.service.cwgl.kmys;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.kmys.CykmdmysDao;
import com.hnkywl.sts.entity.cwgl.kmys.Cykmdmys;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-17
 * Time: 上午10:28
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CykmdmysService {
    private CykmdmysDao cykmdmysDao;

    public String findCykmdmysList(Long gsId,String kmdm,int pageNo,int pageSize){
        Page<Map> page = cykmdmysDao.findCykmdmysList(gsId,kmdm,pageNo,pageSize);
        List result = page.getResult() != null ? page.getResult() : new ArrayList();
        return "{\"total\":"+page.getTotalCount()+",\"rows\":"+new Gson().toJson(result)+"}";
    }

    public String findSlCykmdmysList(Long gsId){
        List<Cykmdmys> cykmdmysList = cykmdmysDao.findSlCykmdmysList(gsId);
        String slJson = BaseUtils.fetchCollectionToJson(cykmdmysList, "id", "jqcwkmmc");
        return "["+slJson+"]";
    }

    /**
     * 保存pmId和codeId的对应关系
     * @param kmysList
     * @param codId
     */
    @Transactional
    public void saveKmys(List<String> kmysList,Long codId){
        deleteAll(codId);
        for(String kmys:kmysList){
            if(StringUtils.isEmpty(kmys)){
                continue;
            }
            String[] items = kmys.split(":");
            if(Long.valueOf(items[1]) == 1){
                saveYjwzbmKmys(Long.valueOf(items[0]),codId);
            } else if(Long.valueOf(items[1]) == 2){
                saveRjwzbmKmys(Long.valueOf(items[0]),codId);
            } else {
                savePmkmys(Long.valueOf(items[0]),codId);
            }
        }
        deleteRepetition();
    }

    /**
     * 插入前先删除
     * @param codeId
     */
    private void deleteAll(Long codeId){
        String sql = "delete from t_sys_pm_cy where codeid = ?";
        cykmdmysDao.executeSQL(sql,codeId);
    }

    private void saveYjwzbmKmys(Long yjwzId,Long codeId){
        String sql = "insert into t_sys_pm_cy select pm.id,"+codeId+" from t_sys_pm pm" +
                "  left join t_sys_bmb pmbm on pm.code = pmbm.bmz left join t_sys_rjwzbm rj on pm.rjwzbm_id = rj.id " +
                "  left join t_sys_bmb rjbm on rjbm.bmz = rj.code left join t_sys_yjwzbm yj on rj.yjwzbm_id = yj.id" +
                " where yj.id = ? and rj.deleted = 0 and pm.deleted = 0 and rjbm.zt = '1' and pmbm.zt = '1'";
        cykmdmysDao.executeSQL(sql,yjwzId);
    }

    private void saveRjwzbmKmys(Long rjwzId,Long codeId){
        String sql = "insert into t_sys_pm_cy select pm.id, "+codeId+" from t_sys_pm pm" +
                "  left join t_sys_bmb pmbm on pm.code = pmbm.bmz left join t_sys_rjwzbm rj on pm.rjwzbm_id = rj.id \n" +
                "where rj.id = ? and rj.deleted = 0 and pm.deleted = 0 and pmbm.zt = '1'";
        cykmdmysDao.executeSQL(sql,rjwzId);
    }

    private void savePmkmys(Long bmId,Long codeId){
        String sql = "insert into t_sys_pm_cy values(?,?)";
        cykmdmysDao.executeSQL(sql,bmId,codeId);
    }

    /**
     * 删除重复项
     */
    @Transactional
    private void deleteRepetition(){
        String sql = "delete from t_sys_pm_cy a where (a.pm_id, a.codeid) in " +
                " (select pm_id, codeid from t_sys_pm_cy group by pm_id, codeid having count(1) > 1) " +
                "and rowid not in (select min(rowid) from t_sys_pm_cy group by pm_id, codeid having count(1) > 1)";
        cykmdmysDao.executeSQL(sql);
    }

    @Transactional
    public void deleteByGsId(Long gsId) {
        cykmdmysDao.deleteByGsId(gsId);
    }

    @Transactional
    public Cykmdmys findKmdmByMc(Long gsId,String mc) {
        return cykmdmysDao.findKmdmByMc(gsId,mc);
    }


    @Transactional
    public void batchSave(List<Cykmdmys> list,Long gsId){
        cykmdmysDao.deleteByGsId(gsId);
        for (Cykmdmys c : list){
            cykmdmysDao.save(c);
        }
    }

    @Transactional
    public void save(Cykmdmys cykmdmys){
        Cykmdmys c = cykmdmysDao.get(cykmdmys.getId());
        c.setYu8cwkmmc(cykmdmys.getYu8cwkmmc());
        c.setJqcwkmmc(cykmdmys.getJqcwkmmc());
        c.setKmdm(cykmdmys.getKmdm());
        c.setSl(cykmdmys.getSl());
        cykmdmysDao.save(c);
    }

    @Autowired
    public void setCykmdmysDao(CykmdmysDao cykmdmysDao) {
        this.cykmdmysDao = cykmdmysDao;
    }
}
